/*
 * Water.h
 *
 *  Created on: 23.10.2012
 *      Author: Martin
 *
 *      Water in the scene
 *      ctor
 *      	high of water
 *      	size of mash
 *      	path to water/ dir in textures
 *      	path to water/ dir in shaders
 */

#pragma once

#include "ShaderPool.h"

#ifdef _WIN32 
#include <windows.h> 
#endif

#include "TexPool.h"
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <string>
#include <vector>
#include "scene/World.h"
#include "special/VBOIndexer.h"
#include "engine/engineSettings.h"
#include "engine/RTSException.h"

class Water
{
public:
	// texture constructor
	// input - high - y position of water surface
	//		 - size - size of water surface
	//		 - path to texture
	//		 - shader pool
	//		 - world class (matrix, light setup etc.)
	Water(float _high, float _size, int _div);
	virtual ~Water();

	// drwa water surface
	void draw(GLuint refTexture, GLuint refractionTexture);
	void draw(GLuint refTexture, GLuint refractionTexture, GLuint v, GLuint n,
			GLuint uv, GLuint e, int isize);

	// create new buffers
	// vertex, normal, uvs and indexing
	// using vertexbuffer, uvbuffer, normalbuffer, elementbuffer
	void createBufferObject(GLuint *v, GLuint *n, GLuint *uv, GLuint *e);

	// send data from indexed_vertices, indexed_uvs,
	// indexed_normals and indices to the buffers
	// prepare buffers to draw
	// return count of elements in element buffer
	int fillBufferObject(GLuint v, GLuint n, GLuint uv, GLuint e);

	// return y position of water surface
	float getHigh();

	// set only one rectangle
	int setVectors(float x, float z, float s);
	void indexing();
private:
	// set vertex, uv and normal to vectors
	void setVertex(float x, float z);

	// set whole water surface
	int setVectors();

	// high and size of water surface
	float high, size;
	int div;

	// texture path
	std::string texPath;

	// texture pool
	TexPool *textures;

	// storede ID
	GLuint shaderID;
	GLuint normalTextureID;

	// buffers
	GLuint VertexArrayID;
	GLuint uvbuffer; // uv buffer ID generated with genUVBO function
	GLuint normalBufferID; // normal buffer identificator
	GLuint elementBufferID;

	std::vector<glm::vec3> indexed_vertices, buf_vertices;
	std::vector<glm::vec2> indexed_uvs, buf_uvs;
	std::vector<glm::vec3> indexed_normals, buf_normals;
	std::vector<unsigned short> indices; // indexing

	// count of elements after indexing
	int iSizeOld;
};

